﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using Orid.Management.Statistics.Tools.Decomposition.Model;
using Orid.Data.SqlServer;

namespace Orid.Management.Statistics.Tools.Decomposition.Data
{
    class Distribute : DataBase
    {
        #region 构造函数 sql 内容
        protected StringBuilder sqlText = new StringBuilder(), sqlDistribute = new StringBuilder();
          

        public Distribute()
        {  
            sqlText.Append(" SELECT DISTINCT [Distributes].[JobID] ");
            sqlText.Append("        ,[Distributes].[OrderID] ");
            sqlText.Append("        ,[Distributes].[JobClassID] ");  
            sqlText.Append("        ,[Distributes].[DownloadID] ");
            sqlText.Append("        ,[Distributes] .[AccountID] ");
            sqlText.Append(" FROM @distributesTableName [Distributes]  with(NOLOCK)");//readpast
            sqlText.Append(" WHERE    [Distributes].[OrderID] = @OrderID ");
            sqlText.Append(" ORDER BY [Distributes].[OrderID],[Distributes].[JobID] ,DownloadID ");

            sqlDistribute.Append(" SELECT TOP 1 [Distributes].[DownloadDateTime]");
            sqlDistribute.Append(" ,[Distributes].[UploadDateTime]");
            sqlDistribute.Append(" FROM  @distributesTableName [Distributes]  with(NOLOCK)");//readpast
            sqlDistribute.Append(" WHERE  [Distributes].[OrderID] = @OrderID ");
            sqlDistribute.Append(" AND  [Distributes].[JobID] = @JobID ");
            sqlDistribute.Append(" AND  [Distributes].[DownloadID] = @DownloadID ");




        }
        #endregion

        #region 虚函数

        public virtual List<ModelJob> GetJobsFromDistributes(Guid OrderID)
        {
            return null;
        }
        #endregion

        #region 方法实现

        protected List<Orid.Management.Statistics.Tools.Decomposition.Model.ModelJob> GetJobsFromDistributes(Guid OrderId, string connectString, string distributesTableName, Account account, UploadJobStore uploadJobStore)
        {
            if (OrderId == null)
            {
                return null;
            }

            List<Orid.Management.Statistics.Tools.Decomposition.Model.ModelJob> listModel = new List<Orid.Management.Statistics.Tools.Decomposition.Model.ModelJob>();
 
            sqlText.Replace("@distributesTableName", distributesTableName);

            using (System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(connectString))
            {

                conn.Open();
                System.Data.SqlClient.SqlParameter[] SqlParameter;
                SqlParameter = new System.Data.SqlClient.SqlParameter[]
                                 { 
                                    new System.Data.SqlClient.SqlParameter("@OrderID",System.Data.SqlDbType.UniqueIdentifier), 
                                    };


                SqlParameter[0].Value = OrderId;
                SqlDataReader orderReader = SqlHelper.ExecuteReader(conn, System.Data.CommandType.Text, sqlText.ToString(), SqlParameter);
                listModel = InitJobModel(orderReader, account, uploadJobStore);
                if (conn.State == System.Data.ConnectionState.Open)
                    conn.Close();


                GetTimeFromDistributes(ref listModel, connectString, distributesTableName);
            }

            return listModel;
        }

        private void GetTimeFromDistributes(ref List< ModelJob> listModelJob, string connectString, string distributesTableName)
        { 
            this.sqlDistribute.Replace("@distributesTableName", distributesTableName);
          
            foreach (ModelJob item in listModelJob)
	        {

                using (System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(connectString))
                {

                    conn.Open();
                    System.Data.SqlClient.SqlParameter[] SqlParameter;
                    SqlParameter = new System.Data.SqlClient.SqlParameter[]
                                     { 
                                        new System.Data.SqlClient.SqlParameter("@OrderID",item.OrderID), 
                                        new System.Data.SqlClient.SqlParameter("@JobID",item.JobID),
                                        new System.Data.SqlClient.SqlParameter("@DownloadID",item.DownLoadID), 
 

                                        };

                    SqlDataReader orderReader = SqlHelper.ExecuteReader(conn, System.Data.CommandType.Text,sqlDistribute.ToString(), SqlParameter);
                    ModelJob modelJob = InitJobTime(orderReader);
                    if (conn.State == System.Data.ConnectionState.Open)
                        conn.Close();

                    item.DownloadDateTime = modelJob.DownloadDateTime;
                    item.UploadDateTime = modelJob.UploadDateTime;
                    item.PropagationTime = modelJob.PropagationTime;

                    ////2010-4-7  效果不明显
                    ////用uploadJobstore 表的UploadDateTime作为job上传时间
                    //if ((item.DownloadDateTime != null) && (item.UploadDateTime != null))
                    //    item.PropagationTime = GetCost((DateTime)item.DownloadDateTime, (DateTime)item.UploadDateTime);


        

                }
            }
            
        }


        protected List<ModelJob> InitJobModel(SqlDataReader reader, Account account, UploadJobStore uploadJobStore)
        {
            List<ModelJob> listModelJob = new List<ModelJob>();

            try
            {
                while (reader.Read())
                {
                    ModelJob item = new ModelJob();

                    item.JobID = (Guid)reader["JobID"];
                 
                    item.OrderID = (Guid)reader["OrderID"];
                    item.JobClassID = (int)reader["JobClassID"];

                    item.DownLoadID = (int)reader["DownLoadID"];
              
                    //这个时间有重复的 一个job 有多个时间
                    //if (reader["DownloadDateTime"] != DBNull.Value)
                    //    item.DownloadDateTime = (DateTime)reader["DownloadDateTime"];

                    //if (reader["UploadDateTime"] != DBNull.Value)
                    //    item.UploadDateTime = (DateTime)reader["UploadDateTime"];
                    int accountID = 0;

                    if (reader["AccountID"]!=DBNull.Value)
                    {
                        accountID = (int)reader["AccountID"];
                        item.AccountID = accountID;
                        item.LoginName = account.GetLoginName( accountID  );
                    }
 
                    ModelUploadJobStore modelUploadJobStore = uploadJobStore.GetEntryDuration(item.JobID, accountID);

                    item.JobDuration = modelUploadJobStore.EntryDuration;
                    item.UploadDateTime = modelUploadJobStore.UploadDateTime;

                    
                    
                  
                   
                    listModelJob.Add(item);
                }
                reader.Close();
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return listModelJob;
        }

        protected  ModelJob  InitJobTime(SqlDataReader reader)
        {
            ModelJob  modelJob = new  ModelJob ();

            try
            {
                while (reader.Read())
                {


                    if (reader["DownloadDateTime"] != DBNull.Value)
                        modelJob.DownloadDateTime = (DateTime)reader["DownloadDateTime"];

                    //上传时间修改成用uploadjobstore里面的上传时间
                    if (reader["UploadDateTime"] != DBNull.Value)
                        modelJob.UploadDateTime = (DateTime)reader["UploadDateTime"];

                    if ((modelJob.DownloadDateTime != null) && (modelJob.UploadDateTime != null))
                        modelJob.PropagationTime = GetCost((DateTime)modelJob.DownloadDateTime, (DateTime)modelJob.UploadDateTime);

                }
                reader.Close();
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return modelJob;
        }



        #endregion
    }
}
